﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;

using MiniSched.Orm;
using MiniSched.Orm.Enumerations;
using MiniSched.Web.Areas.Api.Models;

namespace MiniSched.Web.Areas.Api.Controllers {

    public class OrganizationApiController : EntityApiController<Organization, OrganizationRequestModel> {

        internal override Organization Create(Organization model) {
            if (null == model) throw new ArgumentNullException("model");

            var organization = new Organization {
                OrganizationName = model.OrganizationName
            };

            var userProfile = GetUserProfile();

            var volunteer = new Volunteer {
                FormattedName = userProfile.UserName,
                Organization = organization,
                UserProfile = userProfile
            };

            var userRole = new UserRole {
                Organization = organization,
                Role = Context.Roles.GetOrCreate(RoleOption.SuperAdmin),
                UserProfile = userProfile
            };

            var context = Context;
            context.Organizations.Add(organization);
            context.Volunteers.Add(volunteer);
            context.UserRoles.Add(userRole);

            return organization;
        }

        internal override void Update(Organization entity, Organization model) {
            if (null == model) throw new ArgumentNullException("model");
            if (null == entity) throw new ArgumentNullException("entity");

            entity.OrganizationName = model.OrganizationName;
        }
    }
}
